package de.toolschmied.jjas.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextArea;

import de.toolschmied.jjas.client.cms.actions.Alignment;
import de.toolschmied.jjas.client.cms.actions.AlignmentAction.AlignmentListener;
import de.toolschmied.jjas.client.cms.actions.AlignmentToolbar;
import de.toolschmied.jjas.client.cms.actions.EditingToolbar;
import de.toolschmied.jjas.client.cms.actions.HeadingType;
import de.toolschmied.jjas.client.cms.actions.HeadingTypeToolbar;
import de.toolschmied.jjas.client.cms.actions.EditingToolbar.EditingListener;
import de.toolschmied.jjas.client.cms.actions.HeadingTypeAction.HeadingTypeListener;
import de.toolschmied.jjas.client.widgets.toolbar.PopupToolbar;


public class EditableText extends SimplePanel {

	private final MouseClickHandler clickHandler = new MouseClickHandler();
	private final Toolbar toolbar = new Toolbar();
	
	private final HTML html = new HTML();
	private final TextArea textArea = new TextArea();
	
	private String text;
	private Alignment alignment;
	
	public EditableText(Alignment alignment, String text) {
		super();
		
		setText(text);
		setAlignment(alignment);
		setTextType();
		
		html.addClickHandler(clickHandler);

		setWidget(html);
	}

	private void setAlignment(Alignment alignment) {
		this.alignment = alignment;
		textArea.setAlignment(alignment.textAlignment);
		getElement().getStyle().setTextAlign(alignment.textAlign);
	}
	
	private void setText(String text) {
		this.text = text;
		updateLabel();
	}

	private void setTextType() {
		String styleName = "input-p";
		textArea.setStylePrimaryName(styleName);
		html.setStylePrimaryName(styleName);
		updateLabel();
	}

	private void updateLabel() {
		String labelText = text.replaceAll("\n|\r", "<br/>"); 
		labelText = "<p>" + labelText + "</p>";
		html.setHTML(labelText);
	}

	private void startEditing() {
		textArea.setText(text);
		textArea.setWidth(html.getOffsetWidth() + "px");
		setWidget(textArea);
		textArea.setFocus(true);
		textArea.selectAll();
		showPopupToolbar();
	}
	
	private void showPopupToolbar() {
		toolbar.setSelection(alignment);
		toolbar.showBelow(this);
	}
	
	public void saveEditing() {
		String updateText = textArea.getText();
		setText(updateText);
		setWidget(html);
		hidePopupToolbar();
	}
	
	public void cancelEditing() {
		setWidget(html);
		hidePopupToolbar();
	}
	
	private void hidePopupToolbar() {
		toolbar.hide();
	}
	
	private class MouseClickHandler implements ClickHandler {
		@Override
		public void onClick(ClickEvent event) {
			startEditing();
		}
	}

	private class Toolbar extends PopupToolbar implements AlignmentListener, EditingListener {

		private final AlignmentToolbar alignmentToolbar = new AlignmentToolbar(this);
		private final EditingToolbar editingToolbar = new EditingToolbar(this);
		
		private Toolbar() {
			addToolbar(alignmentToolbar);
			addToolbar(editingToolbar);
		}

		public void setSelection(Alignment alignment) {
			alignmentToolbar.setSelection(alignment);
		}
		
		@Override
		public void saveEditing() {
			EditableText.this.saveEditing();
		}

		@Override
		public void cancelEditing() {
			EditableText.this.cancelEditing();
		}

		@Override
		public void alignmentChanged(Alignment alignment) {
			setAlignment(alignment);
		}

	}

}
